package com.immediatus.graphics.texture.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import com.immediatus.graphics.contracts.IBitmapTextureAtlasSource;

import java.io.IOException;
import java.io.InputStream;

public class AssetBitmapTextureAtlasSource implements IBitmapTextureAtlasSource{

    private int _texturePositionX;
    private int _texturePositionY;

    private final int _width;
    private final int _height;

    private final String _assetPath;
    private final Context _context;

    public AssetBitmapTextureAtlasSource(final Context context_, final String assetPath_){
        this(context_, assetPath_, 0, 0);
    }

    public AssetBitmapTextureAtlasSource(final Context context_, final String assetPath_, final int texturePositionX_, final int texturePositionY_){
        this._texturePositionX = texturePositionX_;
        this._texturePositionY = texturePositionY_;
        this._context = context_;
        this._assetPath = assetPath_;

        final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
        decodeOptions.inJustDecodeBounds = true;

        InputStream in = null;
        try{
            in = context_.getAssets().open(assetPath_);
            BitmapFactory.decodeStream(in, null, decodeOptions);
        } catch (final IOException e){
        } finally{
            try{
                if(in != null)
                    in.close();
            } catch (final IOException e){
                e.printStackTrace();
            }
        }

        this._width = decodeOptions.outWidth;
        this._height = decodeOptions.outHeight;
    }

    public AssetBitmapTextureAtlasSource(final Context context_, final String assetPath_, final int texturePositionX_, final int texturePositionY_, final int width_, final int height_){
        this._texturePositionX = texturePositionX_;
        this._texturePositionY = texturePositionY_;
        this._context = context_;
        this._assetPath = assetPath_;
        this._width = width_;
        this._height = height_;
    }

    @Override
    public AssetBitmapTextureAtlasSource deepCopy(){
        return new AssetBitmapTextureAtlasSource(this._context, this._assetPath, this._texturePositionX, this._texturePositionY, this._width, this._height);
    }

    @Override
    public int getTexturePositionX(){
        return this._texturePositionX;
    }

    @Override
    public int getTexturePositionY(){
        return this._texturePositionY;
    }

    @Override
    public void setTexturePositionX(final int texturePositionX_){
        this._texturePositionX = texturePositionX_;
    }

    @Override
    public void setTexturePositionY(final int texturePositionY_){
        this._texturePositionY = texturePositionY_;
    }

    @Override
    public int getWidth(){
        return this._width;
    }

    @Override
    public int getHeight(){
        return this._height;
    }

    @Override
    public Bitmap onLoadBitmap(final Config bitmapConfig_){
        InputStream in = null;
        try{
            final BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
            decodeOptions.inPreferredConfig = bitmapConfig_;
            in = this._context.getAssets().open(this._assetPath);
            return BitmapFactory.decodeStream(in, null, decodeOptions);
        } catch (final IOException e){
            return null;
        } finally{
            try{
                if(in != null)
                    in.close();
            } catch (final IOException e){
                e.printStackTrace();
            }
        }
    }
}